package me.fangx.zhihu.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.fangx.zhihu.R;
import me.fangx.zhihu.modle.bean.CookBean;
import me.fangx.zhihu.ui.activity.CookDetailActivity;
import me.fangx.zhihu.utils.BaseUtil;

/**
 * Created by fangxiao on 16/1/28.
 */
public class CookListAdapter extends RecyclerView.Adapter<CookListAdapter.ViewHolder> {
    public ArrayList<CookBean> datas = new ArrayList<>();

    Context context;

    public CookListAdapter(Context context, ArrayList<CookBean> datas) {
        this.context = context;
        this.datas = datas;
    }

    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cook_list_item, viewGroup, false);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, CookDetailActivity.class);
                intent.putExtra(CookDetailActivity.ARG_ITEM_INFO, datas.get(position));
                intent.putExtra("position", position);
                context.startActivity(intent);
            }
        });

        CookBean cookBean = datas.get(position);
        viewHolder.ivCookPicture.setImageResource(BaseUtil.getDrawableId(cookBean.getType(), cookBean.getPictureId()));
        viewHolder.tvCookName.setText(cookBean.getName());
        viewHolder.tvCookMaterial.setText(cookBean.getMaterial().replace("\\n", "\n\n"));
        if(cookBean.getIsCollect()==0){
            viewHolder.tvCollect.setVisibility(View.GONE);
        }else{
            viewHolder.tvCollect.setVisibility(View.VISIBLE);
        }

    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return datas.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.iv_cook_picture)
        ImageView ivCookPicture;
        @Bind(R.id.tv_cook_name)
        TextView tvCookName;
        @Bind(R.id.tv_cook_material)
        TextView tvCookMaterial;
        @Bind(R.id.tv_collect)
        TextView tvCollect;

        View itemView;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            this.itemView = view;
        }
    }
}